package org.csu.code.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.csu.code.api.common.CommonResponse;
import org.csu.code.api.entity.*;
import org.csu.code.api.persistence.*;
import org.csu.code.api.service.ManagerService;
import org.csu.code.api.service.OrderService;
import org.csu.code.api.util.MD5Utils;
import org.csu.code.api.vo.AccountVO;
import org.csu.code.api.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("managerService")
public class ManagerServiceImpl implements ManagerService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderStatusMapper orderStatusMapper;

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private SignonMapper signonMapper;

    @Autowired
    private ProfileMapper profileMapper;

    @Override
    public CommonResponse<String> enableItem(String itemId) {
        Item item = itemMapper.selectById(itemId);
        item.setStatus(1);
        itemMapper.update(item, Wrappers.<Item>query().eq("itemid",itemId));
        return CommonResponse.createForSuccess("上架成功");
    }

    @Override
    public CommonResponse<Item> editItem(Item item) {
        itemMapper.updateById(item);
        return CommonResponse.createForSuccess("修改商品成功",item);
    }

    @Override
    public CommonResponse<String> updateAccount(AccountVO accountVO) {
        Account account = new Account();
        account.setPhone(accountVO.getPhone());
        account.setLastName(accountVO.getLastName());
        account.setFirstName(accountVO.getFirstName());
        account.setEmail(accountVO.getEmail());
        account.setCountry(accountVO.getCountry());
        account.setCity(accountVO.getCity());
        account.setAddress2(accountVO.getAddress2());
        account.setAddress1(accountVO.getAddress1());
        account.setZip(accountVO.getZip());
        account.setProvince(accountVO.getProvince());
        account.set_admin(accountVO.is_admin());
        account.set_farmer(accountVO.is_farmer());

        Signon signon = new Signon();
        signon.setUsername(accountVO.getUsername());
        String password = accountVO.getPassword();
        // 将密码进行MD5加密
        password= MD5Utils.md5(password);
        signon.setPassword(password);

        Profile profile = new Profile();
        profile.setFavouriteCategoryId(accountVO.getFavouriteCategoryId());
        profile.setLanguagePreference(accountVO.getLanguagePreference());

        int rows = accountMapper.update(account,Wrappers.<Account>query().eq("userid",accountVO.getUserid()));
        int rows1 = signonMapper.update(signon,Wrappers.<Signon>query().eq("userid",accountVO.getUserid()));
        int rows2 = profileMapper.update(profile,Wrappers.<Profile>query().eq("userid",accountVO.getUserid()));

        if (rows == 0||rows1 == 0||rows2 == 0){
            return CommonResponse.createForError("更新失败");
        }

        return  CommonResponse.createForSuccess("更新成功");
    }
}
